package com.qijian.quality.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.utils.DateUtils;
import com.qijian.common.utils.StringUtils;
import com.qijian.common.utils.uuid.IdUtils;
import com.qijian.quality.domain.InspectionResultRecordDetail;
import com.qijian.quality.domain.InspectionResultRecordDetailValue;
import com.qijian.quality.domain.query.InspectionResultRecordQuery;
import com.qijian.quality.domain.vo.InspectionResultRecordDetailVo;
import com.qijian.quality.domain.vo.InspectionResultRecordVo;
import com.qijian.quality.service.IInspectionResultRecordDetailService;
import com.qijian.quality.service.IInspectionResultRecordDetailValueService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.qijian.quality.mapper.InspectionResultRecordMapper;
import com.qijian.quality.domain.InspectionResultRecord;
import com.qijian.quality.service.IInspectionResultRecordService;

/**
 * 质检结果记录Service业务层处理
 *
 * @author qijian
 * @date 2025-03-28
 */
@Service
@RequiredArgsConstructor
public class InspectionResultRecordServiceImpl extends ServiceImpl<InspectionResultRecordMapper, InspectionResultRecord> implements IInspectionResultRecordService {

    private final InspectionResultRecordMapper inspectionResultRecordMapper;
    private final IInspectionResultRecordDetailService inspectionResultRecordDetailService;
    private final IInspectionResultRecordDetailValueService inspectionResultRecordDetailValueService;

    @Override
    public Page<InspectionResultRecordVo> pageVo(Page<InspectionResultRecord> page, QueryWrapper<InspectionResultRecordQuery> query) {
        return inspectionResultRecordMapper.pageVo(page, query);
    }

    @Override
    public Boolean savePatrolInspection(InspectionResultRecordVo inspectionResultRecordVo) {
        //获取当前报工的检验内容
        List<InspectionResultRecordDetailVo> detailVoList = inspectionResultRecordVo.getDetailVoList();

        List<InspectionResultRecordDetail> saveDetailList = new ArrayList<>();
        List<InspectionResultRecordDetailValue> saveDetailValueList = new ArrayList<>();
        if (detailVoList != null && detailVoList.size() > 0) {
            //总表
            InspectionResultRecord inspectionResultRecord = new InspectionResultRecordVo();
            inspectionResultRecord.setSourceId(inspectionResultRecordVo.getWorkOrderDetailId());
            inspectionResultRecord.setSourceType("PatrolInspection");


            boolean isQualified = true;
            for (InspectionResultRecordDetailVo detailVo : detailVoList) {
                Boolean isText = detailVo.getIsText();
                List<String> actualValueList = detailVo.getActualValueList();
                if (isText) {//填值
                    String maxValue = detailVo.getMaxValue();
                    String minValue = detailVo.getMinValue();
                    for (String actualValue : actualValueList) {
                        if (StringUtils.isEmpty(actualValue)) {
                            continue;
                        }
                        if (isQualified) {
                            if (new BigDecimal(actualValue).compareTo(new BigDecimal(maxValue)) > 0 ||
                                    new BigDecimal(minValue).compareTo(new BigDecimal(actualValue)) > 0) {
                                detailVo.setIsQualified(false);
                                isQualified = false;
                                break;
                            }
                        }
                    }
                } else {
                    if (isQualified) {
                        isQualified = detailVo.getIsQualified();
                    }
                }
                InspectionResultRecordDetail inspectionResultRecordDetail = new InspectionResultRecordDetail();
                BeanUtils.copyProperties(detailVo, inspectionResultRecordDetail);
                inspectionResultRecordDetail.setId(IdUtils.singletonSnowId());
                saveDetailList.add(inspectionResultRecordDetail);
                for (String s : actualValueList) {
                    InspectionResultRecordDetailValue inspectionResultRecordDetailValue = new InspectionResultRecordDetailValue();
                    inspectionResultRecordDetailValue.setId(IdUtils.singletonSnowId());
                    inspectionResultRecordDetailValue.setActualValue(s);
                    inspectionResultRecordDetailValue.setDetailId(inspectionResultRecordDetail.getId());
                    saveDetailValueList.add(inspectionResultRecordDetailValue);
                }
            }

            inspectionResultRecord.setIsQualified(isQualified);
            save(inspectionResultRecord);
            for (InspectionResultRecordDetail detail : saveDetailList) {

                detail.setMainId(inspectionResultRecord.getId());
            }
            inspectionResultRecordDetailService.saveBatch(saveDetailList);
            inspectionResultRecordDetailValueService.saveBatch(saveDetailValueList);
        }
        return true;
    }

    @Override
    public List<InspectionResultRecordDetailVo> getInspectionData(Long id) {
        return inspectionResultRecordMapper.getInspectionData(id);
    }
}
